﻿using System.Drawing;
using System.Windows.Forms;
using Sinchron.Properties;

namespace Sinchron
{
    public partial class ConfigurationForm : Form
    {
        private Color? newDayColour;
        private Color? newHourColour;
        private Color? newMinuteColour;
        private Color? newSecondColour;

        public ConfigurationForm()
        {
            InitializeComponent();

            this.Load += new System.EventHandler(ConfigurationForm_Load);
        }

        void ConfigurationForm_Load(object sender, System.EventArgs e)
        {
            DayColourButton.Text = Settings.Default.DayColour.Name;
            HourColourButton.Text = Settings.Default.HourColour.Name;
            MinuteColourButton.Text = Settings.Default.MinuteColour.Name;
            SecondColourButton.Text = Settings.Default.SecondColour.Name;
        }

        internal void CommitChanges()
        {
            if (newDayColour.HasValue)
            {
                Settings.Default.DayColour = newDayColour.Value;
            }
            if (newHourColour.HasValue)
            {
                Settings.Default.HourColour = newHourColour.Value;
            }
            if (newMinuteColour.HasValue)
            {
                Settings.Default.MinuteColour = newMinuteColour.Value;
            }
            if (newSecondColour.HasValue)
            {
                Settings.Default.SecondColour = newSecondColour.Value;
            }
            Settings.Default.Save();
        }

        private bool GetColorFromDialog(ref Color? colour)
        {
            var colorDialog = new ColorDialog();

            if (colorDialog.ShowDialog() == DialogResult.OK)
            {
                colour = colorDialog.Color;

                return true;
            }
            else
            {
                return false;
            }
        }

        private void DayColourButton_Click(object sender, System.EventArgs e)
        {
            if (GetColorFromDialog(ref this.newDayColour))
            {
                DayColourButton.Text = newDayColour.Value.Name;
            }
        }

        private void HourColourButton_Click(object sender, System.EventArgs e)
        {
            if (GetColorFromDialog(ref this.newHourColour))
            {
                HourColourButton.Text = newHourColour.Value.Name;
            }
        }

        private void MinuteColourButton_Click(object sender, System.EventArgs e)
        {
            if (GetColorFromDialog(ref this.newMinuteColour))
            {
                MinuteColourButton.Text = newMinuteColour.Value.Name;
            }
        }

        private void SecondColourButton_Click(object sender, System.EventArgs e)
        {
            if (GetColorFromDialog(ref this.newSecondColour))
            {
                SecondColourButton.Text = newSecondColour.Value.Name;
            }
        }
    }
}
